package org.red.checkSystem.service.impl.check;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.red.checkSystem.pojo.Address;
import org.red.checkSystem.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.red.checkSystem.service.check.checkCRUDService;
import org.red.checkSystem.mapper.articleMapper;
import org.red.checkSystem.service.check.checkAutoService;
import org.red.checkSystem.mapper.addressMapper;
import java.util.Date;

@Service
public class checkCRUDServiceImpl implements checkCRUDService {
    @Autowired
    articleMapper articleMapper;
    @Autowired
    addressMapper addressMapper;
    @Autowired
    checkAutoService checkAutoService;
    @Override
    public String addArticleUnCheck(Article article,String address) {
        String t = checkAutoService.checkAuto(article);
        if(t.equals("error")){
            article.setState("含有违法内容");
            articleMapper.insert(article);
            return "success";
        }
        else {
            article.setState("待审核");
            articleMapper.insert(article);
        }
        if(!address.isEmpty()){
            QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("max(id) as id");
            int max = articleMapper.selectOne(queryWrapper).getId();
            Address address1 = new Address();
            address1.setAddress(address);
            address1.setAid(max);
            addressMapper.insert(address1);
        }
        return "success";
    }

    @Override
    public String checkArticle(int id,String state,String admin) {
        Article article = articleMapper.selectById(id);
        article.setState(state);
        article.setAdmin(admin);
        article.setChecktime(new Date());
        articleMapper.updateById(article);
        return "success";
    }

    @Override
    public String getAuthor(int id) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return articleMapper.selectOne(queryWrapper).getAuthor();
    }

    @Override
    public String deteleArticle(int id) {
        articleMapper.deleteById(id);
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("aid",id);
        addressMapper.delete(queryWrapper);
        return null;
    }

    @Override
    public String updataArticle(Article article,String address) {
        article.setState("待审核");
        article.setTime(new Date());
        articleMapper.updateById(article);
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("aid",article.getId());
        Address address1 = addressMapper.selectOne(queryWrapper);
        if(article.getTag().equals("红色景点")){
            if(address1 != null){
                addressMapper.updateById(address1);
                address1.setAddress(address);
            }
            else {
                Address t = new Address();
                t.setAid(article.getId());
                t.setAddress(address);
                addressMapper.insert(t);
            }
        }
        return "success";
    }
}
